package com.own.component.store.local.util;

import com.own.component.store.core.util.base.BaseLockMethod;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * LocalLockStoreUtil
 *
 * @author chenxueli
 * @date 2023-07-10 15:51:55
 */
@Slf4j
public class LocalLockStoreUtil implements BaseLockMethod {

    private final Map<String, Lock> LOCK_MAP = new ConcurrentHashMap<>();

    /**
     * 获取锁对象
     *
     * @param key 锁的key
     * @return 锁
     */
    @Override
    public Lock getLock(String key) {
        return LOCK_MAP.computeIfAbsent(key, k -> new ReentrantLock());
    }

}
